package cn.jane.bigevent.service.impl;

import cn.jane.bigevent.entity.Article;
import cn.jane.bigevent.entity.PageBean;
import cn.jane.bigevent.mapper.ArticleMapper;
import cn.jane.bigevent.service.ArticleService;
import cn.jane.bigevent.util.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import java.time.LocalDateTime;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {

    private final ArticleMapper articleMapper;

    public ArticleServiceImpl(ArticleMapper articleMapper) {
        this.articleMapper = articleMapper;
    }

    @Override
    public void add(Article article) {
        article.setCreateUser(ThreadLocalUtil.getUserId());
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(int pageNum, int pageSize, Integer categoryId, String state) {
        PageBean<Article> res = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        Page<Article> page = (Page<Article>) articleMapper.list(categoryId, state, ThreadLocalUtil.getUserId());
        res.setTotal(page.getTotal());
        res.setItems(page.getResult());
        return res;
    }

    @Override
    public Article findById(int id) {
        return articleMapper.findById(id);
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }

    @Override
    public void delete(int id) {
        articleMapper.delete(id);
    }
}
